Standard features including data type <b>bool</b>, cast
operators, <b>namespace</b>s, run-time type information
(RTTI) and operator keywords. We also discuss
pointers-to-class-member operators and <b>virtual</b> base
classes--which are not new ANSI C++ features. This
chapter was co-authored with Mr. Tem Nieto, the
authors' colleague at Deitel & Associates, Inc.<br>
</page>
</section>
<section type=Body name=Default title="21.2 bool Data Type">
<page>
<font size=18 bold>21.2 <b>bool</b> Data Type</font><hr>
The ANSI/ISO C++ draft standard provides data type
<b>bool</b> whose values may be <b>false</b> or <b>true</b> as a
preferred alternative to the old style of using <b>0</b> to
indicate false and nonzero to indicate true. The program
of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 21.1</a> demonstrates data type <b>bool</b>.<br>
<spacer width=16 height=1>Line 9 <br>
<font size=2><br></font><font size=11><pre>
bool boolean = false;<p>
</pre></font>
declares variable <b>boolean</b> to be of type <b>bool</b> and
initializes <b>boolean</b> to <b>false</b>. Variable <b>x</b> is declared
and initialized to <b>0</b>. Line 12<br>
<font size=2><br></font><font size=11><pre>
cout << "boolean is " << boolean<p>
</pre></font>
</page>
<page>
outputs <b>boolean</b>'s value. The value <b>0</b> is output rather
than the keyword <b>false</b>. Numeric values are the
default display for <b>bool</b>s. <br>
<spacer width=16 height=1>The value of <b>x</b> (input on line 14) is used as an <b>if</b>/
<b>else</b>'s condition in line 20. If <b>x</b> is <b>0</b>, the condition is
<b>false</b>. Otherwise the condition is <b>true</b>. Note that
negative values are nonzero and therefore <b>true</b>.<br>
<spacer width=16 height=1>Line 25 assigns <b>true</b> to <b>boolean</b>. The value of
<b>boolean</b> (<b>1</b>) is output on line 26. A <b>bool</b> variable
outputs as <b>0</b> or <b>1</b> by default. The stream insertion
operator <b><<</b> has been overloaded to display <b>bool</b>s as
integers.<br>
<spacer width=16 height=1>Lines 27 and 28<br>
</page>
<page>
<font size=2><br></font><font size=11><pre>
cout << "\nboolean output with boolalpha<p>
manipulator is " <p>
<< boolalpha << boolean << endl;<p>
</pre></font>
uses the stream manipulator <b>boolalpha</b> to set the
output stream to display <b>bool</b> values as the strings
"<b>true</b>" and "<b>false</b>." Manipulator <b>boolalpha</b> can
also be used on input.<br>
<spacer width=16 height=1>Pointers, <b>int</b>s, <b>float</b>s, etc. can be implicitly
converted to <b>bool</b>s. Zero <a href="^Practice::c:s0p1"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>values convert to <b>false</b>
and nonzero values convert to <b>true</b>. For example the
expression<br>
<font size=2><br></font><font size=11><pre>
bool dc = false + x * 2 - b && true;<p>
</pre></font>
</page>
<page>
would assign <b>true</b> to <b>dc</b> assuming <b>x</b> is <b>3</b>, and <b>b</b> is
<b>true</b>. Note that the right-hand portion of the
assignment expression evaluates to <b>5</b>, but this value is
implicitly <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>converted to <b>true</b>. <br>
</page>
<page>
<b>Select the true statement(s). </b><br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. Manipulator boolalpha can be used with input.">
Manipulator boolalpha cannot be used with input. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. Variables of type bool are displayed as integer values of 1 (true) and 0 (false) by default.">
The stream insertion operator has been overloaded to display bools as "true" or "false" by default. <br>
C++ provides the <b>reinterpret_cast</b> operator for
nonstandard casts (e.g. <b>void</b> <b>*</b> to <b>int</b>, etc.). Operator
<b>reinterpret_cast</b> can also be used for standard
casts (i.e., <b>double</b> to <b>int</b>, etc.). The program of <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig.
21.4</a> demonstrates the use of the
<b>reinterpret_cast</b> operator.<br>
<spacer width=16 height=1>The <a href="^Portable::c:s0p0"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>program declares an integer and three pointers.
Pointer <b>voidPtr</b> is initialized to the address of
<b>unsigned</b> <b>x</b> and pointer <b>charPtr</b> is initialized to
the C-style string "<b>C++</b>". Line 12<br>
</page>
<page>
<font size=2><br></font><font size=11><pre>
unsignedPtr = reinterpret_cast< unsigned * ><p>
( voidPtr );<p>
</pre></font>
uses <a href="^Debug::c:s0p0"><img src="bckgrnds/icons/dbt_ico.gif" align=sidebar></a>operator <b>reinterpret_cast</b> to cast
<b>voidPtr</b> (of type <b>void</b> <b>*</b>) to <b>unsigned</b> <b>*</b> pointer
<b>unsignedPtr</b>. <br>
<spacer width=16 height=1>Lines 18 and 19<br>
<font size=2><br></font><font size=11><pre>
cout << "\nchar * to unsigned results in: "<p>
<< ( x = reinterpret_cast< unsigned ><p>
( charPtr ) );<p>
</pre></font>
output the result of casting <b>charPtr</b> to <b>unsigned</b>.
The result assigned to <b>x</b> represents the decimal address
location of the string "<b>C++</b>". <br>
<spacer width=16 height=1>Lines 22 and 23<br>
</page>
<page>
<font size=2><br></font><font size=11><pre>
cout << "\nunsigned to char * results in: "<p>
<< reinterpret_cast< char * >( x ) << endl;<p>
</pre></font>
cast the value of <b>x</b> to <b>char</b> <b>*</b>. The result is an address
(<b>char</b> <b>*</b>), which results in the output of the C-style
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. The possibility of one namespace colliding with another namespace always exists.">
keywords (<a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 21.8</a>) that can be used in place of several
C++ operators. Operator keywords can be useful for
programmers keyboards that do not support certain
characters such as <b>!</b>, <b>&</b>, <b>^</b>, <b>~</b>, <b>|</b>, etc.<br>
<spacer width=16 height=1>The program of <a href="^Code::c:s0p7">Fig.<img src="bckgrnds/icons/code_ico.gif" align=sidebar> 21.9</a> demonstrates the use of the
operator keywords. This program was compiled with
Microsoft Visual C++ 5.0 which requires the header file
<b><iso646.h></b> to use the operator keywords. Other
compilers may differ, so check documentation for your
compiler to determine the header file to include (the <br>
</page>
<page>
compiler may not require any header file to use these
keywords).<br>
<spacer width=16 height=1>The program declares and initializes two integers <b>a</b> and
<b>b</b>. Logical and bitwise operations are performed with <b>a</b>
and <b>b</b> using the various operator keywords. The result
of each operation is output.<br>
</page>
<page>
<b>Drag the correct operator to the box associated with
In Chapter 8, "Operator Overloading," we discussed
that any constructor that is called with one argument
can be used by the compiler to perform an implicit
conversion in which the type received by the
constructor is converted to an object of the class in
which the constructor is defined. The conversion is
automatic and the programmer need not use a cast
operator. In some situations implicit conversions are
undesirable or error-prone. For example, our <b>Array</b>
class in Fig. 8.4 defines a constructor that takes a single
<b>int</b> argument. The intent of this constructor is to create <br>
</page>
<page>
an <b>Array</b> object containing the number of elements
specified by the <b>int</b> argument. However, this
constructor can be misused by the compiler to perform
an implicit conversion. The program of <a href="^Code::c:s0p8"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 21.10</a> uses
a simplified version of class <b>Array</b> from Chapter 8 to
demonstrate an improper implicit conversion.<br>
<spacer width=16 height=1>Line 60 in main<br>
<font size=2><br></font><font size=11><pre>
Array integers1( 7 );<p>
</pre></font>
defines <b>Array</b> object <b>integers1</b> and calls the single
argument constructor with the <b>int</b> value <b>7</b> to specify
the number of elements in the <b>Array</b>. We modified the
<b>Array</b> constructor so it outputs a line of text indicating <br>
</page>
<page>
that the <b>Array</b> constructor was called and the number
of elements that were allocated in the <b>Array</b>. Line 62<br>
<font size=2><br></font><font size=11><pre>
outputArray( integers1 ); <p>
// output Array integers1<p>
</pre></font>
calls function <b>outputArray</b> (defined at line 69) to
output the contents of the <b>Array</b>. Function
<b>outputArray</b> receives as its argument a <b>const</b>
<b>Array</b> <b>&</b> to the <b>Array</b>, then outputs the <b>Array</b> using
the overloaded stream insertion operator <b><<</b>. Line 64<br>
<font size=2><br></font><font size=11><pre>
outputArray( 15 ); <p>
// convert 15 to an Array and output <p>
</pre></font>
calls function <b>outputArray</b> with the <b>int</b> value <b>15</b>
as an argument. There is no function <b>outputArray</b> <br>
</page>
<page>
that takes an <b>int</b> argument, so the compiler checks
class <b>Array</b> to determine if there is a conversion
constructor that can convert an <b>int</b> into an <b>Array</b>.
Because class <b>Array</b> provides a conversion
constructor, the compiler uses that constructor to create
a temporary <b>Array</b> object containing <b>15</b> elements and
passes the temporary <b>Array</b> object to function
<b>outputArray</b> to output the <b>Array</b>. The output
shows that the <b>Array</b> conversion constructor was
called for a <b>15</b> element <b>Array</b> and the contents of the
<b>Array</b> were output.<br>
<spacer width=16 height=1>C++ provides the keyword <tt><i>explicit</i></tt> to suppress
implicit conversions via conversion constructors. A <br>
</page>
<page>
constructor that is declared <b>explicit</b> cannot be used
in an implicit conversion. The program of <a href="^Code::c:s0p9"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 21.11</a>
demonstrates an <b>explicit</b> constructor.<br>
<spacer width=16 height=1>The only modification to the program of <a href="^Code::c:s0p8"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 21.10</a> was
the addition of the keyword <b>explicit</b> to the
declaration of the single-argument constructor at line
11. When the program is compiled, the compiler
produces an error message indicating that the integer
value passed to <b>outputArray</b> at line 64 cannot be
converted to a <b>const</b> <b>Array</b> <b>&</b>. The compiler error
message is shown in the output window. Line 66
illustrates <a href="^Errors::c:s0p4"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>how to create an <b>Array</b> of <a href="^Errors::c:s0p5"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>15 elements and <br>
</page>
<page>
pass it to <b>outputArray</b> using <a href="^Engineer::c:s0p4"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>the <b>explicit</b>
Single argument constructors can be used by the compiler to perform implicit conversions. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. Constructors declared can only be used to initialize objects of their class type.">
An explicit single argument constructor can be used by the compiler to perform implicit conversions.explicit <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="21.10 mutable Class Members">
<page>
<font size=18 bold>21.10 <b>mutable</b> Class Members</font><hr>
In<a href="#s4p0"> Section 21.4</a>, we introduced the <b>const_cast</b>
operator which allowed "<b>const</b>-ness" to be cast away.
C++ provides the storage class specifier <b>mutable</b> as
an alternative to <b>const_cast</b>. A <b>mutable</b> data
member is always modifiable even in a <b>const</b> member
function or <b>const</b> object. This reduces the <a href="^Portable::c:s0p1"><img src="bckgrnds/icons/port_ico.gif" align=sidebar></a>need to cast
away "<b>const</b>-ness."<br>
<spacer width=16 height=1>Both <b>mutable</b> and <b>const_cast</b> allow a data
member to be modified; each is used in different
contexts. For a <b>const</b> object with no <b>mutable</b> data
members, operator <b>const_cast</b> must be used every <br>
</page>
<page>
time a member is to be modified. This greatly reduces
the chance of a member being accidently modified
because the member is not permanently modifiable.
Operations involving <b>const_cast</b> are typically
hidden in a member function's implementation. The
user of a class may not be aware that a <a href="^Engineer::c:s0p5"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>member is being
modified.<br>
<spacer width=16 height=1>The program of <a href="^Code::c:s0p10"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 21.12</a> demonstrates using a
<b>mutable</b> member.<br>
<spacer width=16 height=1>The program defines class <b>TestMutable</b> (line 5) that
contains a constructor, two functions and <b>private</b>
<b>mutable</b> data member <b>value</b>. Line 8<br>
<font size=2><br></font><font size=11><pre>
void modifyValue() const { value++; }<p>
</pre></font>
</page>
<page>
defines function <b>modifyValue</b> as a <b>const</b> function
that increments <b>mutable</b> data member <b>value</b>.
Normally, a <b>const</b> member function cannot modify
data members unless the object on which the function
operates--i.e., to one to which <b>this</b> points--is cast
(using <b>const_cast</b>) to a non-<b>const</b> type. Because
<b>value</b> is <b>mutable</b>, this <b>const</b> function is able to
modify the data. Function <b>getValue</b> (line 9) is a
<b>const</b> function that returns <b>value</b>. Note <b>getValue</b>
could change <b>value</b> because <b>value</b> is <b>mutable</b>. <br>
<spacer width=16 height=1>Line 16 declares <b>const</b> <b>TestMutable</b> object <b>t</b> and
initializes it to <b>99</b>. Line 18 outputs the contents of
<b>value</b>. Line 20 calls the <b>const</b> member function <br>
</page>
<page>
<b>modifyValue</b> to add one to <b>value</b>. Note that both <b>t</b>
and <b>modifyValue</b> are <b>const</b>. Line 21 outputs the
contents of <b>value</b> (<b>100</b>) to prove that the <b>mutable</b>
<b>DerivedTwo</b>) is the most derived class. If creating a
<b>Multiple</b> object, <b>Multiple</b> is the most derived
class. No matter how far down the hierarchy a class is,
it is therefore the most derived class and responsible for
initializing the <b>virtual</b> base class. In<a href="^Exercises::c:s0p18"> <img src="bckgrnds/icons/exercise.gif" align=sidebar>Exercise 21.17</a>
we ask the reader to exercise <a href="^Engineer::c:s0p6"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>the concept of most
derived class.<br>
</page>
<page>
<b>Select the true statement(s). </b><br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. Inheritance hierarchies are capable of working when diamond inheritance occurs. Sometimes portions of the hierarchy must be redone. ">
When diamond inheritance occurs, the whole inheritance hierarchy must be redesigned. <br>
<indent width=8 delay>* The ANSI/ISO C++ draft standard provides data type
<tt><b>bool</b></tt> (with values of <tt><b>false</b></tt> or <tt><b>true</b></tt>) as a preferred
alternative to the old style of using <b>0</b> to indicate false
and nonzero to indicate true. </indent>
<indent width=8 delay>* The stream manipulator <tt><b>boolalpha</b></tt> sets the output
stream to display <b>bool</b> values as the strings "<b>true</b>"
and "<b>false</b>." </indent>
<indent width=8 delay>* The ANSI/ISO C++ draft standard introduces four
new cast operators to use in preference to "old-style"
casting used in C and C++. </indent>
<indent width=8 delay>* C++ provides the <tt><b>static_cast</b></tt> operator for con</indent>
</page>
<page>
<indent width=8 delay>* version between types. Type checking is performed at
compile time. </indent>
<indent width=8 delay>* The <tt><b>const_cast</b></tt> operator casts away the <b>const</b>-
ness of objects.</indent>
<indent width=8 delay>* The <b>reinterpret_cast</b> operator is provided for
nonstandard casts (e.g. <b>void</b> <b>*</b> to <b>int</b>, etc.) between
unrelated types.</indent>
<indent width=8 delay>* Each <b>namespace</b> defines a scope where global
identifiers and global variables are placed. To use a
<b>namespace</b> member, the member's name must be
qualified with the <b>namespace</b> name and the binary
scope resolution operator (<b>::</b>) or a <b>using</b> statement
must occur before the name is used.</indent>
</page>
<page>
<indent width=8 delay>* A <b>namespace</b> can contain constants, data, classes,
nested <b>namespace</b>s, functions, etc. Definitions of
<b>namespace</b>s must occupy the global scope or be
nested within other <b>namespace</b>s.</indent>
<indent width=8 delay>* Unnamed <b>namespace</b> members occupy the global
<tt><b>namespace</b></tt>. </indent>
<indent width=8 delay>* Run-time type information (RTTI) provides a means
of determining an object's type at run time. </indent>
<indent width=8 delay>* The compile-time operator <b>typeid</b> returns a reference to a <b>type_info</b> object. A <b>type_info</b> object is
a system maintained object that represents a type. </indent>
<indent width=8 delay>* The <tt><b><typeinfo.h></b></tt> (<tt><b><typeinfo></b></tt> in ANSI/ISO
draft standard C++) header file which defines <b>typeid</b>. </indent>
</page>
<page>
<indent width=8 delay>* Operator <b>dynamic_cast</b> is used in polymorphic
programming to ensure that proper conversions take
place at run time. The result of a <b>dynamic_cast</b> is <b>0</b>
if the for invalid cast operations.</indent>
<indent width=8 delay>* The ANSI/ISO C++ draft standard provides operator
keywords (<a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 21.8</a>) that can be used in place of several C++ operators. </indent>
<indent width=8 delay>* C++ provides the keyword <tt><b>explicit</b></tt> to suppress
implicit conversions via conversion constructors. A
constructor that is declared <b>explicit</b> cannot be used
in an implicit conversion. </indent>
<indent width=8 delay>* A <b>mutable</b> data member is always modifiable even
in a <b>const</b> member function or <b>const</b> object. </indent>
</page>
<page>
<indent width=8 delay>* C++ provides the<b>.*</b> and <b>->*</b> operators for accessing
class members via pointers to those members. </indent>
<indent width=8 delay>* Multiple inheritance can lead to the problem of duplicate subobjects. This is resolved with <b>virtual</b> inheritance. When a base class is inherited as <b>virtual</b>,
only one subobject will appear in the derived class--a
process called virtual base class inheritance. </indent>
<a href="^Illustration::c:s0p2">Fig. 21.14</a> Multiple inheritance to form class <b>iostream</b>.<br>
<a href="^Code::c:s0p12">Fig. 21.15</a> Attempting to call a multiply inherited function polymorphically.<br>
<a href="^Code::c:s0p13">Fig. 21.16</a> Using <b>virtual</b> base classes.<br>
<br>
</page>
<page>
<font size=18><a href="~audio/Ch21/21fig008.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 21.8 - Operator keywords as alternatives to operator symbols. <img src="graphics/ch21/fig21008.gif" ></font><br>
</page>
<page>
<font size=18>Fig<a href="~audio/Ch21/21fig014.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>ure 21.14 - Multiple inheritance to form class <b>iostream</b>.<img src="graphics/ch21/fig21014.gif" ></font><br>